<template>
  <div id="assignEditorDialog">
    <el-dialog :visible.sync="visible" v-if="visible" :title="title" width="600px">
      <el-row>
        <el-col :span="12">
          <span>当前过滤条件下子任务数量: 共计{{ taskItemList.length }}条</span>
        </el-col>
        <el-col :span="12">
          <el-radio-group v-model="type">
            <el-radio-button :label="0">指定分配</el-radio-button>
            <el-radio-button :label="1">平均分配</el-radio-button>
          </el-radio-group>
        </el-col>
      </el-row>
      <el-row>

        <el-table highlight-current-row :data="appointEditorList"
                  border
                  stripe>
          <el-table-column label="子任务范围"
                           v-if="type === 0"
                           min-width="250">
            <template slot-scope="scope">
              <el-input-number style="width: 45%;"
                               v-model="scope.row.startIndex"
                               controls-position="right"
                               :min="1" :max="scope.row.endIndex"
                               size="small"/>
              <span>至</span>
              <el-input-number style="width: 45%;"
                               v-model="scope.row.endIndex"
                               controls-position="right"
                               :min="scope.row.startIndex" :max="taskItemList.length"
                               size="small"/>
            </template>
          </el-table-column>

          <el-table-column key="editorCol" prop="editor"
                           label="编辑"
                           min-width="100" v-if="title === '分配编辑'">
            <template slot-scope="scope">
              <el-select filterable v-model="scope.row.editor" style="width: 100%"
                         placeholder="请选择"
                         size="small">
                <el-option
                  v-for="(item,index) in editorList"
                  :key="index"
                  :label="item.name"
                  :value="item.userName">
                </el-option>
              </el-select>
            </template>
          </el-table-column>

          <el-table-column key="auditorCol" prop="auditor"
                           label="审核"
                           min-width="100" v-else>
            <template slot-scope="scope">
              <el-select filterable v-model="scope.row.editor" style="width: 100%"
                         placeholder="请选择"
                         size="small">
                <el-option
                  v-for="(item,index) in auditorList"
                  :key="index"
                  :label="item.name"
                  :value="item.userName">
                </el-option>
              </el-select>
            </template>
          </el-table-column>

          <el-table-column label="操作"
                           width="100">
            <template slot-scope="scope">
              <span><el-link type="danger"
                             @click.prevent="appointEditorList.splice(scope.$index, 1)">删除</el-link></span>
            </template>
          </el-table-column>
        </el-table>
        <el-row>
          <div style="text-align: center; border: solid 1px #e6e9f3; height: 48px; line-height: 48px;">
            <el-link style="color: #2db7a0" @click="addClick"><i class="el-icon-plus"></i>新增分组</el-link>
          </div>
        </el-row>
      </el-row>

      <span class="dialog-footer" slot="footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button @click="commit" type="primary">确 定</el-button>
      </span>

    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "AssignEditorDialog",
  props: ['taskItemList', 'taskId'],
  data() {
    return {
      appointEditorList: [],
      title: '分配编辑',
      visible: false,
      type: 0,
    }
  },
  computed: {
    editorList() {
      return this.$store.state.userList.filter(user => user.status === 1)
    },
    auditorList() {
      return this.editorList
    },
  },
  watch: {
    visible() {
      this.appointEditorList = []
    }
  },
  methods: {
    addClick() {
      // 任务范围, startIndex 为上一级的endIndex + 1
      let startIndex = 1
      if (this.appointEditorList.length > 0) {
        startIndex = this.appointEditorList[this.appointEditorList.length - 1].endIndex + 1
      }
      let tableDataItem = {
        startIndex: startIndex,
        endIndex: this.taskItemList.length,
        editor: '',
        startDate: '',
        endDate: '',
        remark: '',
      }
      this.appointEditorList.push(tableDataItem)
    },
    commit() {
      // 指定分配;
      if (this.type === 0) {
        for (let i = 1; i < this.appointEditorList.length; i++) {
          if (this.appointEditorList[i].startIndex <= this.appointEditorList[i - 1].endIndex) {
            this.$message.warning("任务范围有重复, 请重新设置")
            return;
          }
        }
        for (let appointEditor of this.appointEditorList) {
          for (let i = appointEditor.startIndex - 1; i < appointEditor.endIndex; i++) {
            if (this.title === '分配编辑') {
              this.taskItemList[i].editor = appointEditor.editor
            } else if (this.title === '分配初审') {
              this.taskItemList[i].firstAuditor = appointEditor.editor
            } else if (this.title === '分配复审') {
              this.taskItemList[i].secondAuditor = appointEditor.editor
            } else if (this.title === '分配终审') {
              this.taskItemList[i].thirdAuditor = appointEditor.editor
            }
            // this.taskItemList[i].remark = appointEditor.remark
          }
        }
      }
      // 平均分配;
      if (this.type === 1) {
        // 分配逻辑, 每次取当前剩余未分配的总数和当前未分配的人员 取 / 运算, 保证平均分配结果最多相差一个;
        // 例如 8 个子任务, 分配给 3人, 第一人分配  Math.ceil(8/3)=3个, 第二人分配 Math.ceil(5/2)=3个, 第三人分配剩下2个;
        let endIndex = 0;  // 当前分配的最后子任务下标
        for (let i = 0; i < this.appointEditorList.length; i++) {
          let appointEditor = this.appointEditorList[i]
          let count = Math.ceil((this.taskItemList.length - endIndex) / (this.appointEditorList.length - i)) // 应该分配给当前用户的任务数
          // 把下标为 endIndex -> endIndex + count 的子任务分配给当前用户;
          for (let j = 0; j < count; j++) {
            if (this.title === '分配编辑') {
              this.taskItemList[endIndex + j].editor = appointEditor.editor
            } else if (this.title === '分配初审') {
              this.taskItemList[endIndex + j].firstAuditor = appointEditor.editor
            } else if (this.title === '分配复审') {
              this.taskItemList[endIndex + j].secondAuditor = appointEditor.editor
            } else if (this.title === '分配终审') {
              this.taskItemList[endIndex + j].thirdAuditor = appointEditor.editor
            }
            // this.taskItemList[endIndex + j].remark = appointEditor.remark
          }
          endIndex += count
        }
      }
      // 修改后台
      if (this.taskId) {
        this.axios.post('/pmph/action/task/updateTaskItemList', this.taskItemList)
          .then(() => {
            this.visible = false
          })
      } else {
        this.visible = false
      }
    }
  },
}
</script>

<style scoped>
#assignEditorDialog >>> .el-dialog__body {
  padding: 0px 20px;
}

</style>
